package com.corn.juc.pool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2021/1/5 11:44
 */

public class ExecutorArgumentConfigDemo {

    public static void main(String[] args) {
        cpu();
        io();
    }

    /**
     * CPU密集型配置
     */
    private static void cpu() {
        int cpuNumber = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(cpuNumber + 1, cpuNumber + 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3));
        executor.shutdown();
    }

    /**
     * io 密集型
     */
    private static void io() {
        int cpuNumber = Runtime.getRuntime().availableProcessors();
        final double blockCoefficient = 0.9D;
        final int corePoolSize = (int) (cpuNumber / (1 - blockCoefficient));
        System.out.println(corePoolSize);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize, corePoolSize, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(3));
        executor.shutdown();
    }
}
